package com.example.mydy.withdraw;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mydy.R;

public class WithdrawStep3Fragment extends Fragment implements WithdrawStepListener {

    private TextView tvConfirmAmount;
    private TextView tvServiceFee;
    private TextView tvActualAmount;
    private TextView tvConfirmAlipayAccount;
    private TextView tvConfirmAlipayName;
    private CheckBox checkboxAgreement;
    private TextView tvAgreementText;
    
    private WithdrawDataManager dataManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_withdraw_step3, container, false);
        
        // 初始化视图
        tvConfirmAmount = view.findViewById(R.id.tv_confirm_amount);
        tvServiceFee = view.findViewById(R.id.tv_service_fee);
        tvActualAmount = view.findViewById(R.id.tv_actual_amount);
        tvConfirmAlipayAccount = view.findViewById(R.id.tv_confirm_alipay_account);
        tvConfirmAlipayName = view.findViewById(R.id.tv_confirm_alipay_name);
        checkboxAgreement = view.findViewById(R.id.checkbox_agreement);
        tvAgreementText = view.findViewById(R.id.tv_agreement_text);
        
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 获取数据管理器
        dataManager = WithdrawDataManager.getInstance();
        
        // 加载确认信息
        loadConfirmationData();
        
        // 设置协议点击事件
        setupAgreementClickListener();
    }

    private void setupAgreementClickListener() {
        // 为协议文本添加点击事件，显示协议内容
        tvAgreementText.setOnClickListener(v -> {
            showAgreementDialog();
        });
    }

    private void showAgreementDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle("提现服务协议");
        
        // 提现协议内容
        String agreementText = "提现服务协议\n\n" +
                "一、服务定义\n" +
                "1.1 本服务是指用户通过本平台将账户余额提现至指定的支付宝账户。\n\n" +
                "二、服务规则\n" +
                "2.1 用户必须提供真实、准确的支付宝账户信息，包括账号和实名信息。\n" +
                "2.2 单笔提现金额不得低于1元人民币。\n" +
                "2.3 提现申请提交后将在1-3个工作日内进行审核和处理。\n" +
                "2.4 提现可能产生服务费，具体费率以页面显示为准。\n\n" +
                "三、用户责任\n" +
                "3.1 用户应确保提供的支付宝账户为本人所有，因提供错误账户信息导致的资金损失由用户自行承担。\n" +
                "3.2 用户应遵守国家相关法律法规，不得利用本服务进行洗钱、诈骗等违法活动。\n\n" +
                "四、平台责任\n" +
                "4.1 平台将尽力保障提现服务的正常运行，但因银行系统、支付宝系统等第三方原因导致的提现延迟或失败，平台不承担责任。\n" +
                "4.2 平台有权对存在风险的提现申请进行核实和拦截。\n\n" +
                "五、其他事项\n" +
                "5.1 本协议解释权归本平台所有。\n" +
                "5.2 如有争议，请联系客服解决。";
        
        builder.setMessage(agreementText);
        builder.setPositiveButton("我已阅读", (dialog, which) -> {
            // 自动勾选复选框
            checkboxAgreement.setChecked(true);
            dialog.dismiss();
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void loadConfirmationData() {
        // 显示提现金额
        double amount = dataManager.getWithdrawAmount();
        tvConfirmAmount.setText(String.format("¥ %.2f", amount));
        
        // 显示服务费
        double serviceFee = dataManager.getServiceFee();
        tvServiceFee.setText(String.format("¥ %.2f", serviceFee));
        
        // 显示实际到账金额
        double actualAmount = dataManager.getActualAmount();
        tvActualAmount.setText(String.format("¥ %.2f", actualAmount));
        
        // 显示支付宝账户信息
        String alipayAccount = dataManager.getAlipayAccount();
        String alipayName = dataManager.getAlipayName();
        
        if (!TextUtils.isEmpty(alipayAccount)) {
            // 显示部分隐藏的支付宝账号
            if (alipayAccount.length() > 7) {
                String maskedAccount = alipayAccount.substring(0, 3) + "****" + 
                        alipayAccount.substring(alipayAccount.length() - 4);
                tvConfirmAlipayAccount.setText(maskedAccount);
            } else {
                tvConfirmAlipayAccount.setText(alipayAccount);
            }
        } else {
            tvConfirmAlipayAccount.setText("未设置");
        }
        
        if (!TextUtils.isEmpty(alipayName)) {
            // 显示部分隐藏的姓名
            if (alipayName.length() > 1) {
                String maskedName = alipayName.substring(0, 1) + "*" + 
                        (alipayName.length() > 2 ? alipayName.substring(alipayName.length() - 1) : "");
                tvConfirmAlipayName.setText(maskedName);
            } else {
                tvConfirmAlipayName.setText(alipayName);
            }
        } else {
            tvConfirmAlipayName.setText("未设置");
        }
    }

    @Override
    public boolean validate() {
        // 验证是否同意协议
        if (!checkboxAgreement.isChecked()) {
            Toast.makeText(requireContext(), "请阅读并同意《提现服务协议》", Toast.LENGTH_SHORT).show();
            return false;
        }
        
        return true;
    }
} 